<template>
	<view class="">
		<view class="nav-box" v-if="state == 2">
			<view class="background"
				:style="{ background: backgroundColor, height: backgroundColor == '#008efe' ? 44 + statusBarHeight + 'px' : '400rpx' }">
			</view>
			<view class="seat-box" :style="{ height: 44 + statusBarHeight + 'px' }"></view>
			<view class='nav-wrap'
				:style="{ background: backgroundColor != 'linear-gradient(to bottom, #0076DE 60%, #fefefe)' ? backgroundColor : '#0076DE;' }">
				<view :style="{ height: statusBarHeight + 'px' }"></view>
				<view class="content" v-if="is_logo == ''">
					<view class="title" :style="{ lineHeight: '44px', color: titleColor }">{{ navTitle }}</view>
					<view v-if="isShow" class="capsule-box" :class="[!isBack ? 'noBack' : '']"
						:style="{ height: capsule.height + 'px', position: 'absolute', top: capsule.top - statusBarHeight + 'px', left: left + 'px' }">
						<block v-if="localIsBack">
							<view class="capsult-item" @click="back" :data-num="pageNum">
								<uni-icons type="left" size="40rpx" :color="titleColor"></uni-icons>
							</view>
						</block>
						<block v-else>
							<view class="capsult-item" @click="toIndex">
								<uni-icons type="home-filled" size="30" :color="titleColor"></uni-icons>
							</view>
						</block>
					</view>
				</view>
				<view class="content" v-else>
					<view class="capsule-box"
						:style="{ height: capsule.height + 'px', position: 'absolute', top: capsule.top - statusBarHeight + 'px', left: left + 'px' }">
						<image class='logo' :src="is_logo" mode="heightFix">
						</image>
					</view>
				</view>
			</view>
		</view>
		<view class="nav-box" v-if="state != 2">
			<view class="seat-box" :style="{ height: 44 + statusBarHeight + 'px' }"></view>
			<view class='nav-wrap'
				:style="{ background: backgroundColor != 'linear-gradient(to bottom, #0076DE 60%, #fefefe)' ? backgroundColor : '#0076DE;' }">
				<view :style="{ height: statusBarHeight + 'px' }"></view>
				<view class="content">
					<view class="flex">
						<view class="input">
							<image class="search" src="https://hfd.bjlzkj.com/static/index/search.png" mode=""></image>
							<input v-model="keyword" type="text" placeholder="请输入搜索内容" />
						</view>
						<view class="btn" @tap="search">
							搜索
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	name: "navView",
	props: {
		seat: {
			type: Boolean,
			default: true
		},
		// 是否显示左侧按钮
		isShow: {
			type: Boolean,
			default: true
		},
		// 是否显示左侧返回按钮
		isBack: {
			type: Boolean,
			default: false
		},
		// 返回层级 默认为1
		pageNum: {
			type: Number,
			default: 1
		},
		titleColor: {
			type: String,
			default: '#fff'
		},
		navTitle: {
			type: String,
			default: ''
		},
		is_logo: {
			type: String,
			default: ''
		},
		backgroundColor: {
			type: String,
			default: 'linear-gradient(to bottom, #0076DE 60%, #fefefe)'
		},
		state: {
			type: Number,
			default: 2
		}
	},
	data() {
		return {
			capsule: {},
			statusBarHeight: "",
			navBarHeight: "",
			left: "",
			keyword: '',
			localIsBack: this.isBack // 新增本地变量
		};
	},
	watch: {
		isBack(newVal) { // 添加prop监听
			this.localIsBack = newVal
		}
	},
	onReady() {
		const app = getApp();
		// 获取机型状态栏信息
		const {
			statusBarHeight,
			navBarHeight,
			capsule
		} = app.globalData;
		console.log(app.globalData);
		this.statusBarHeight = statusBarHeight;
		this.navBarHeight = navBarHeight;
		this.left = app.globalData.windowWidth - capsule.right; // 胶囊据右边距离
		this.capsule = capsule;
		const pages = getCurrentPages();
		console.log(pages)
		this.isBack = pages.length === 1 ? false : true;
	},
	methods: {
		back(e) {
			uni.navigateBack({
				delta: e.currentTarget.dataset.num
			});
		},
		// 跳转到首页
		toIndex() {
			uni.switchTab({
				url: '/pages/index/index'
			});
		},
		search() {
			this.$emit('onSearch', this.keyword)
		}

	}
}
</script>

<style scoped lang="scss">
.background {
	width: 100vw;
	height: 400rpx;
	position: absolute;
	top: 0;
	left: 0;
	z-index: -1;
}

.seat-box {
	width: 100%;
}

.nav-wrap {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	z-index: 99999;
}

.content {
	position: relative;
	width: 100%;
	height: 44px;

	.back {
		width: 44rpx;
		height: 44rpx;
	}

	.capsule-box {
		width: 85px;
		border-radius: 88rpx;
		display: flex;
		position: relative;
		justify-content: space-between;
	}

	.capsult-item {
		display: flex;
		width: 50%;
		align-items: center;
		justify-content: center;
	}

	.logo {
		width: 156rpx;
		height: 74rpx;
		position: absolute;
		left: 20rpx;
	}

	.noBack {
		width: 32px;
	}

	.noBack .capsult-item {
		width: 100%;
	}

	.title {
		text-align: center;
		font-size: 34rpx;
	}

	.capsule-box.isblack {
		border: 1rpx solid rgba(0, 0, 0, .1);
		background: rgba(255, 255, 255, .6);
	}

	.capsule-box.opacity {
		background: rgba(0, 0, 0, .1);
	}

	.lineblack {
		height: 36rpx;
		background: #B0B0B0;
		width: 1rpx;
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		opacity: 0.5;
	}

	.lineWhite {
		height: 36rpx;
		background: rgba(255, 255, 255, .7);
		width: 1rpx;
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		opacity: 0.5;
	}

	.button {
		background-color: #ffffff;
		color: #1890ff;
		border: 1px solid #1890ff;
		border-radius: 5px;
		padding: 5px 10px;
		font-size: 14px;
	}

	.button.active {
		background-color: #1890ff;
		color: #ffffff;
	}

	.flex {
		width: 70%;
		display: flex;
		align-items: center;
		padding: 0 30rpx;

		.input {
			display: flex;
			align-items: center;
			border-radius: 16rpx;
			width: 60%;
			background: #FFFFFF;
			box-shadow: 0rpx 3rpx 6rpx 1rpx rgba(0, 0, 0, 0.16);
			border-radius: 32rpx;
			padding: 10rpx 30rpx;

			.search {
				width: 30rpx;
				height: 30rpx;
				margin-right: 10rpx;
			}

			input {
				width: 80%;
				font-size: 24rpx;
			}
		}

		.btn {
			width: 109rpx;
			height: 64rpx;
			line-height: 64rpx;
			background: #008EFF;
			border-radius: 10rpx;
			font-size: 24rpx;
			color: #FFFFFF;
			margin-left: 20rpx;
			text-align: center;
		}
	}
}
</style>